home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / comm.jar / content / communicator / utilityOverlay.js < prev    next >
Encoding:
Text File  |  2001-07-06  |  11.9 KB  |  401 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. /**
  25.  * Communicator Shared Utility Library
  26.  * for shared application glue for the Communicator suite of applications
  27.  **/
  28.  
  29. /**
  30.  * Go into online/offline mode
  31.  **/
  32.  
  33. const kIOServiceProgID = "@mozilla.org/network/io-service;1";
  34. const kObserverServiceProgID = "@mozilla.org/observer-service;1";
  35.  
  36. function toggleOfflineStatus()
  37. {
  38.   var checkfunc;
  39.   try {
  40.     checkfunc = document.getElementById("offline-status").getAttribute('checkfunc');
  41.   }
  42.   catch (ex) {
  43.     checkfunc = null;
  44.   }
  45.  
  46.   var ioService = Components.classes[kIOServiceProgID]
  47.                             .getService(Components.interfaces.nsIIOService);
  48.   if (checkfunc) {
  49.     if (!eval(checkfunc)) {
  50.       // the pre-offline check function returned false, so don't go offline
  51.       return;
  52.     }
  53.   }
  54.   ioService.offline = !ioService.offline;
  55. }
  56.  
  57. function setOfflineUI(offline)
  58. {
  59.   var broadcaster = document.getElementById("Communicator:WorkMode");
  60.   if (!broadcaster) return;
  61.  
  62.   //Checking for a preference "network.online", if it's locked, disabling 
  63.   // network icon and menu item
  64.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  65.   prefService = prefService.getService();
  66.   prefService = prefService.QueryInterface(Components.interfaces.nsIPrefService);
  67.   
  68.   var prefBranch = prefService.getBranch(null);
  69.   
  70.   var offlineLocked = prefBranch.prefIsLocked("network.online"); 
  71.   
  72.   if (offlineLocked ) {
  73.       broadcaster.setAttribute("disabled","true");
  74.   }
  75.  
  76.   var bundle = srGetStrBundle("chrome://communicator/locale/utilityOverlay.properties");
  77.  
  78.   if (offline)
  79.     {
  80.       broadcaster.setAttribute("offline", "true");
  81.       broadcaster.setAttribute("tooltiptext", bundle.GetStringFromName("offlineTooltip"));
  82.       broadcaster.setAttribute("label", bundle.GetStringFromName("goonline"));
  83.       FillInTooltip(broadcaster);
  84.     }
  85.   else
  86.     {
  87.       broadcaster.removeAttribute("offline");
  88.       broadcaster.setAttribute("tooltiptext", bundle.GetStringFromName("onlineTooltip"));
  89.       broadcaster.setAttribute("label", bundle.GetStringFromName("gooffline"));
  90.       FillInTooltip(broadcaster);
  91.     }
  92. }
  93.  
  94. var goPrefWindow = 0;
  95.  
  96. function getBrowserURL() {
  97.  
  98.   try {
  99.     var prefs = Components.classes["@mozilla.org/preferences;1"];
  100.     if (prefs) {
  101.       prefs = prefs.getService();
  102.       if (prefs)
  103.         prefs = prefs.QueryInterface(Components.interfaces.nsIPref);
  104.     }
  105.     if (prefs) {
  106.       var url = prefs.CopyCharPref("browser.chromeURL");
  107.       if (url)
  108.         return url;
  109.     }
  110.   } catch(e) {
  111.   }
  112.   return "chrome://navigator/content/navigator.xul";
  113. }
  114.  
  115. function goPageSetup()
  116. {
  117. }
  118.  
  119. function goEditCardDialog(abURI, card, okCallback, abCardURI)
  120. {
  121.   window.openDialog("chrome://messenger/content/addressbook/abEditCardDialog.xul",
  122.                       "",
  123.                       "chrome,resizeable=no,modal,titlebar",
  124.                       {abURI:abURI, card:card, okCallback:okCallback, abCardURI:abCardURI});
  125. }
  126.  
  127. function goPreferences(containerID, paneURL, itemID)
  128. {
  129.   var resizable;
  130.   var pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  131.   
  132.   try {
  133.     // We are resizable ONLY if in box debugging mode, because in
  134.     // this special debug mode it is often impossible to see the 
  135.     // content of the debug panel in order to disable debug mode.
  136.     resizable = pref.GetBoolPref("xul.debug.box");
  137.   }
  138.   catch (e) {
  139.     resizable = false;
  140.   }
  141.  
  142.   var resizability = "yes"; // resizable ? "yes" : "no";
  143.   var features = "chrome,titlebar,resizable=" + resizability;
  144.   openDialog("chrome://communicator/content/pref/pref.xul","PrefWindow", 
  145.              features, paneURL, containerID, itemID);
  146. }
  147.  
  148. function goToggleToolbar( id, elementID )
  149. {
  150.   var toolbar = document.getElementById( id );
  151.   var element = document.getElementById( elementID );
  152.   if ( toolbar )
  153.   {
  154.     var attribValue = toolbar.getAttribute("hidden") ;
  155.  
  156.     if ( attribValue == "true" )
  157.     {
  158.       toolbar.setAttribute("hidden", "false" );
  159.       if ( element )
  160.         element.setAttribute("checked","true")
  161.     }
  162.     else
  163.     {
  164.       toolbar.setAttribute("hidden", true );
  165.       if ( element )
  166.         element.setAttribute("checked","false")
  167.     }
  168.     document.persist(id, 'hidden');
  169.     document.persist(elementID, 'checked');
  170.   }
  171. }
  172.  
  173.  
  174. function goClickThrobber( urlPref )
  175. {
  176.   var url;
  177.   try {
  178.     var pref = Components.classes["@mozilla.org/preferences;1"].getService();
  179.     if( pref )
  180.     pref = pref.QueryInterface( Components.interfaces.nsIPref );
  181.     url = pref.getLocalizedUnicharPref(urlPref);
  182.   }
  183.  
  184.   catch(e) {
  185.     url = null;
  186.   }
  187.  
  188.   if ( url )
  189.     openTopWin(url);
  190. }
  191.  
  192.  
  193. //No longer needed.  Rip this out since we are using openTopWin
  194. function goHelpMenu( url )
  195. {
  196.   /* note that this chrome url should probably change to not have all of the navigator controls */
  197.   /* also, do we want to limit the number of help windows that can be spawned? */
  198.   window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  199. }
  200.  
  201.  
  202. function openTopWin( url )
  203. {
  204.   /* note that this chrome url should probably change to not have all of the navigator controls,
  205.      but if we do this we need to have the option for chrome controls because goClickThrobber()
  206.      needs to use this function with chrome controls */
  207.   /* also, do we want to limit the number of help windows that can be spawned? */
  208.     if ((url == null) || (url == "")) return;
  209.  
  210.     // xlate the URL if necessary
  211.     if (url.indexOf("urn:") == 0)
  212.     {
  213.         url = xlateURL(url);        // does RDF urn expansion
  214.     }
  215.  
  216.     // avoid loading "", since this loads a directory listing
  217.     if (url == "") {
  218.         url = "about:blank";
  219.     }
  220.  
  221.     var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();
  222.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  223.  
  224.     var topWindowOfType = windowManagerInterface.getMostRecentWindow( "navigator:browser" );
  225.     if ( topWindowOfType )
  226.     {
  227.         topWindowOfType.focus();
  228.     topWindowOfType._content.location.href = url;
  229.     }
  230.     else
  231.     {
  232.         window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  233.     }
  234. }
  235.  
  236. function goAboutDialog()
  237. {
  238.   var defaultAboutState = false;
  239.   try {
  240.     var pref = Components.classes["@mozilla.org/preferences;1"].getService();
  241.     if( pref )
  242.       pref = pref.QueryInterface( Components.interfaces.nsIPref );
  243.     defaultAboutState = pref.GetBoolPref("browser.show_about_as_stupid_modal_window");
  244.   }
  245.   catch(e) {
  246.     defaultAboutState = false;
  247.   }
  248.   if( defaultAboutState )
  249.     window.openDialog("chrome:global/content/about.xul", "About", "modal,chrome,resizable=yes,height=450,width=550");
  250.   else
  251.     window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", 'about:' );
  252. }
  253.  
  254. // update menu items that rely on focus
  255. function goUpdateGlobalEditMenuItems()
  256. {
  257.   goUpdateCommand('cmd_undo');
  258.   goUpdateCommand('cmd_redo');
  259.   goUpdateCommand('cmd_cut');
  260.   goUpdateCommand('cmd_copy');
  261.   goUpdateCommand('cmd_paste');
  262.   goUpdateCommand('cmd_selectAll');
  263.   goUpdateCommand('cmd_delete');
  264. }
  265.  
  266. // update menu items that rely on the current selection
  267. function goUpdateSelectEditMenuItems()
  268. {
  269.   goUpdateCommand('cmd_cut');
  270.   goUpdateCommand('cmd_copy');
  271.   goUpdateCommand('cmd_delete');
  272. }
  273.  
  274. // update menu items that relate to undo/redo
  275. function goUpdateUndoEditMenuItems()
  276. {
  277.   goUpdateCommand('cmd_undo');
  278.   goUpdateCommand('cmd_redo');
  279. }
  280.  
  281. // update menu items that depend on clipboard contents
  282. function goUpdatePasteMenuItems()
  283. {
  284.   goUpdateCommand('cmd_paste');
  285. }
  286.  
  287. // This used to be BrowserNewEditorWindow in navigator.js
  288. function NewEditorWindow(aPageURL)
  289. {
  290.   // Open editor window with blank page
  291.   // Kludge to leverage openDialog non-modal!
  292.   window.openDialog( "chrome://editor/content", "_blank", "chrome,all,dialog=no", "about:blank");
  293. }
  294.  
  295. function NewEditorFromTemplate()
  296. {
  297.   // XXX not implemented
  298. }
  299.  
  300. function NewEditorFromDraft()
  301. {
  302.   // XXX not implemented
  303. }
  304.  
  305. // Any non-editor window wanting to create an editor with a URL
  306. //   should use this instead of "window.openDialog..."
  307. //  We must always find an existing window with requested URL
  308. // (When calling from a dialog, "launchWindow" is dialog's "opener"
  309. //   and we need a delay to let dialog close)
  310. function editPage(url, launchWindow, delay)
  311. {
  312.   // User may not have supplied a window
  313.   if (!launchWindow)
  314.   {
  315.     if (window)
  316.     {
  317.       launchWindow = window;
  318.     }
  319.     else
  320.     {
  321.       dump("No window to launch an editor from!\n");
  322.       return;
  323.     }
  324.   }
  325.  
  326.   var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();
  327.   if (!windowManager) return;
  328.   var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  329.   if ( !windowManagerInterface ) return;
  330.   var enumerator = windowManagerInterface.getEnumerator( "composer:html" );
  331.   if ( !enumerator ) return;
  332.  
  333.   while ( enumerator.hasMoreElements() )
  334.   {
  335.     var window = windowManagerInterface.convertISupportsToDOMWindow( enumerator.getNext() );
  336.     if ( window && window.editorShell)
  337.     {
  338.       if (window.editorShell.checkOpenWindowForURLMatch(url, window))
  339.       {
  340.         // We found an editor with our url
  341.         window.focus();
  342.         return;
  343.       }
  344.     }
  345.   }
  346.  
  347.   // Create new Composer window
  348.   if (delay)
  349.     launchWindow.delayedOpenWindow("chrome://editor/content", "chrome,all,dialog=no", url);
  350.   else
  351.     launchWindow.openDialog("chrome://editor/content", "_blank", "chrome,all,dialog=no", url);
  352. }
  353.  
  354. // function that extracts the filename from a url
  355. function extractFileNameFromUrl(urlstr)
  356. {
  357.   if (!urlstr) return null;
  358.   return urlstr.slice(urlstr.lastIndexOf( "/" )+1);
  359. }
  360.  
  361. var offlineObserver = {
  362.   Observe: function(subject, topic, state) {
  363.     // sanity checks
  364.     if (topic != "network:offline-status-changed") return;
  365.     setOfflineUI(state == "offline");
  366.   }
  367. }
  368.  
  369. function utilityOnLoad(aEvent)
  370. {
  371.   var broadcaster = document.getElementById("Communicator:WorkMode");
  372.   if (!broadcaster) return;
  373.  
  374.   var observerService = Components.classes[kObserverServiceProgID]
  375.                   .getService(Components.interfaces.nsIObserverService);
  376.  
  377.   // crude way to prevent registering twice.
  378.   try {
  379.     observerService.RemoveObserver(offlineObserver, "network:offline-status-changed");
  380.   }
  381.   catch (ex) {
  382.   }
  383.   observerService.AddObserver(offlineObserver, "network:offline-status-changed");
  384.   // make sure we remove this observer later
  385.   addEventListener("unload",utilityOnUnload,false);
  386.  
  387.   // set the initial state
  388.   var ioService = Components.classes[kIOServiceProgID]
  389.               .getService(Components.interfaces.nsIIOService);
  390.   setOfflineUI(ioService.offline);
  391. }
  392.  
  393. function utilityOnUnload(aEvent) 
  394. {
  395.   var observerService = Components.classes[kObserverServiceProgID]
  396.               .getService(Components.interfaces.nsIObserverService);
  397.   observerService.RemoveObserver(offlineObserver, "network:offline-status-changed");
  398. }
  399.  
  400. addEventListener("load",utilityOnLoad,true);
  401.